CROSS_COMPILE = aarch64-linux-gnu-
ARCH = arm64
OUTPUT_DIR ?= output
OBJ_PATH ?= objs/
LIBS = dl crypto RockeyARM m
DEPLIB_DIR = deplib

# Set the shell to BASH
SHELL = /bin/bash

# Allow the user to specify cross-compilation from the command line
CC = $(CROSS_COMPILE)gcc
CXX = $(CROSS_COMPILE)g++
LD = $(CROSS_COMPILE)gcc
STRIP = $(CROSS_COMPILE)strip

# Standard gcc flags for compilation
# -DDEBUG_ALG_BARCODE_PRINT 
GLOBAL_CFLAGS = -Wall -D_GNU_SOURCE -march=armv8-a -mtune=cortex-a53 -O1 


APP_SRC_DIRS = appsrc
LIBWXCR_SRC_DIRS = libsrc
LIBWXCR_INC_DIRS = include
LIBWXCR_INC_FLAGS = $(addprefix -I,$(LIBWXCR_INC_DIRS))

# The flags for compiling the library
LIBWXCR_CFLAGS = $(GLOBAL_CFLAGS) -fPIC -shared \
				   -Wno-missing-field-initializers

TARGET  = $(OUTPUT_DIR)/shtest
LIBWXCR_NAME = wxcoder
LIBWXCR_LIBSHARED = $(OUTPUT_DIR)/lib$(LIBWXCR_NAME).so

EXCLUDE_LIB_FILE = 
LIB_DIR_SCAN := $(shell find $(LIBWXCR_SRC_DIRS) -maxdepth 5 -type d)
LIB_SRC_FILES = $(foreach lib_dir_i, $(LIB_DIR_SCAN), $(filter-out $(EXCLUDE_LIB_FILE),$(wildcard $(lib_dir_i)/*.c)))
LIB_ASM_FILES = $(foreach lib_dir_i, $(LIB_DIR_SCAN), $(filter-out $(EXCLUDE_LIB_FILE),$(wildcard $(lib_dir_i)/*.S)))
LIB_SRC_OBJS = $(addprefix $(OBJ_PATH), $(patsubst %.c, %.o, $(LIB_SRC_FILES)))
LIB_ASM_OBJS = $(addprefix $(OBJ_PATH), $(patsubst %.S, %.o, $(LIB_ASM_FILES)))

EXCLUDE_APP_FILE = 
APP_DIR_SCAN := $(shell find $(APP_SRC_DIRS) -maxdepth 5 -type d)
#APP_SRC_FILES = $(foreach app_dir_i, $(APP_DIR_SCAN), $(filter-out $(EXCLUDE_APP_FILE),$(wildcard $(app_dir_i)/*.c)))
APP_SRC_OBJS = $(addprefix $(OBJ_PATH), $(patsubst %.c, %.o, appsrc/sh_test.c))

# None of the targets correspond to actual files
.PHONY: all clean cross_compiler_check prepare_dir tools 
all : cross_compiler_check prepare_dir $(TARGET)
$(LIBWXCR_LIBSHARED): $(LIB_SRC_OBJS) $(LIB_ASM_OBJS)
	$(CC) $(LIBWXCR_CFLAGS) $(LIBWXCR_INC_FLAGS) -L $(DEPLIB_DIR) $(LIB_SRC_OBJS) $(LIB_ASM_OBJS) $(addprefix -l,$(LIBS)) -o $@
#	$(qexec)$(STRIP) -s $@

$(TARGET): $(LIBWXCR_LIBSHARED) $(APP_SRC_OBJS)
	$(LD) $(LIBWXCR_INC_FLAGS) $(GLOBAL_CFLAGS) -L $(OUTPUT_DIR) -L $(DEPLIB_DIR) $(addprefix -l,$(LIBS)) $(addprefix -l,$(LIBWXCR_NAME)) $(APP_SRC_OBJS) -o $@

# Make the specified output directory, if it doesn't exist
prepare_dir:
	@mkdir -p $(OUTPUT_DIR)
	@mkdir -p $(OBJ_PATH)


$(OBJ_PATH)%.o : %.c
	@mkdir -p $(dir $@);
#	@echo $*
	$(CC) -c $(LIBWXCR_CFLAGS) $(LIBWXCR_INC_FLAGS) -o $(OBJ_PATH)$*.o $< 

$(OBJ_PATH)%.o : %.S
	@mkdir -p $(dir $@);
#	@echo $*
	$(CC) -c -fno-PIE -DCC_HAVE_ASM_GOTO -DCONFIG_AS_LSE=1 -mabi=lp64 -Wa,-gdwarf-2 $(LIBWXCR_INC_FLAGS) -o $(OBJ_PATH)$*.o $< 

clean :
	rm -rf $(OBJ_PATH)
	rm -rf $(LIBWXCR_LIBSHARED)
	rm -rf $(TARGET)
	rm -f ./tools/encrypt
	rm -f ./tools/chanpin

tools:
	@rm -f ./tools/encrypt ./tools/chanpin
	$(CC) -L $(DEPLIB_DIR) -lcrypto -o ./tools/encrypt ./tools/encrypt.c $(LIBWXCR_INC_FLAGS) -D_GNU_SOURCE -Wall
	$(CC) -L $(DEPLIB_DIR) -lRockeyARM -o ./tools/chanpin ./tools/chanpin.c $(LIBWXCR_INC_FLAGS) -D_GNU_SOURCE -Wall

# Check that the specified cross-compiler exists
cross_compiler_check:
ifdef CROSS_COMPILE
ifeq (,$(shell which $(CC)))
	@printf "Error: '$(CROSS_COMPILE)' is not a valid cross-compiler prefix. "
	@printf "'$(CC)' was not found in your path.\n"
	@exit 1
endif
endif
